package edu.olin.scheduler.common;
//Annie Martin, March 2007
//Refactored: JStanton,2007apr15

import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;

public class Course implements TreeNode {
    
    //fields
    private String name;
    private String number;
    private double credits;
    private String description;
    private Meeting[] meetings;
    private Category category;
    private String section = null;
    
    //constructors
    public Course(String name, String number, String section, double credits, String description, Meeting[] meetings, Category category){
        //TODO: Input argument checking!
        this.name = name;
        this.number = number;
        this.section = section;
        this.credits = credits;
        this.description = description;
        this.meetings = meetings;
        this.category = category;
        for(int i=0; i<meetings.length; i++){
            meetings[i].setOwner(this);
            }
        }
    public Course(String name, String number, double credits, String description, Meeting[] meetings, Category category){
        this(name,number,"01",credits,description,meetings,category);
        }
    public Course(String name, String number, double credits, String description, Meeting[] meetings){
        this(name,number,credits,description,meetings,null);
        }
    
    public Course(String name, Category category, Meeting[] meetings) {
        this(name,"",4.0,"No description found",meetings,category);
        }
    
    //TreeNode methods
    public Enumeration children(){ return null; }
    public void addChild(TreeNode newChild){ }
    public boolean getAllowsChildren(){ return false; }
    public TreeNode getChildAt(int childIndex){ return null; }
    public int getChildCount(){ return 0; }
    public int getIndex(TreeNode node){ return -1; }
    public TreeNode getParent(){ return category; }
    public boolean isLeaf(){ return false; }
    
    //field accessor methods
    public String getName(){ return name; }
    public String getNumber(){  return number; }
    public double getCredits(){ return credits; }
    public String getDescription(){ return description; }
    public Meeting[] getMeetings(){  return meetings; }
    public Category getCategory() { return category; }
    public void setCategory(Category c){ category = c; }
    public String getSection(){ return section; }
    public void setSection(String section){ this.section = section; }
    
    public SimpleTime getEarliestTime(){
        if(meetings==null){ return null; }
        SimpleTime rtn = null;
        for(int i=0; i<meetings.length; i++){
            if(meetings[i]==null){ continue; }
            if(rtn==null || meetings[i].start().isBefore(rtn)){ rtn = meetings[i].start(); }
            }
        return rtn;
        }
    public SimpleTime getLatestTime(){
        if(meetings==null){ return null; }
        SimpleTime rtn = null;
        for(int i=0; i<meetings.length; i++){
            if(meetings[i]==null){ continue; }
            if(rtn==null || meetings[i].end().isAfter(rtn)){ rtn = meetings[i].end(); }
            }
        return rtn;
        }
    
    }
